package ar.com.mindset.geometrics.legends {
	import ar.com.mindset.geometrics.ColorEntry;
	import ar.com.mindset.geometrics.RegionsLegend;
	
	import com.degrafa.GeometryGroup;
	import com.degrafa.GraphicText;
	import com.degrafa.geometry.RegularRectangle;
	import com.degrafa.paint.SolidFill;
	import com.degrafa.paint.SolidStroke;
	
	import mx.collections.ArrayCollection;

	public class ListLegend extends GeometryGroup implements RegionsLegend{

		public var squareSize:Number=10;
		public var offset:Number=0;
		public var bottomY:Number=450;
		public var spaceBetweenSquares:Number=5;
		public var spaceBeforeText:Number=10;
		public var othersText:String="Others"


		public function ListLegend(x:Number=400) {
			super();
			this.x=x;
		}

		public function initialize(divisions:ArrayCollection, defaultColor:Object):void {

			offset=bottomY - (divisions.length + 1) * (squareSize + spaceBetweenSquares) - spaceBetweenSquares;

			var collection:ArrayCollection=new ArrayCollection();

			addLegendsToCollection(collection, divisions, defaultColor);

			this.geometry=collection.toArray();
		}

		protected function addLegendsToCollection(collection:ArrayCollection, divisions:ArrayCollection, defaultColor:Object):void {
			if (divisions.length > 0) {
				for (var i:Number=0; i < divisions.length; i++) {
					var element:ColorEntry=divisions[i] as ColorEntry;
					addSquare(element.color, element.key.toString(), collection);
				}
				addSquare(defaultColor, othersText, collection);
			}
		}

		protected function addSquare(color:Object, text:String, geometryCollection:ArrayCollection):void {
			var legendItemOutline:SolidStroke=new SolidStroke("#000000", 0.5);

			var regularRectangle:RegularRectangle=new RegularRectangle(0, offset, squareSize, squareSize);
			regularRectangle.stroke=legendItemOutline;
			regularRectangle.fill=new SolidFill(color);

			var graphicText:GraphicText=new GraphicText();
			graphicText.antiAliasType="advanced";
			graphicText.autoSizeField=true;
			graphicText.fontSize=10;
			graphicText.embedFonts=true;
			graphicText.fontFamily="ArialEmbedded";
			graphicText.text=text;
			graphicText.x=regularRectangle.x + regularRectangle.width + spaceBeforeText;
			graphicText.y=offset;

			offset=offset + squareSize + spaceBetweenSquares;


			geometryCollection.addItem(regularRectangle);
			geometryCollection.addItem(graphicText);

		}




	}
}